#pragma once

#include "types.h"

#include <windows.h>
#include "apCritSec.h"

class apFifoBuffer
{
public:
	apFifoBuffer(void);
	~apFifoBuffer(void);

	bool create(uint32 size);
	void reset();
	void close();
	void end();										/// no more data from upstream

	bool	is_empty();
	uint64	offset();
	int32	size();
	int32	used();
	bool	eof();
	byte*	header();
	uint64	totalWrite();

	uint32 write(byte* p, uint32 howmuch);
	uint32 read(byte* p,  uint32 howmuch);
	uint32 skip(uint32 howmuch);

private:
	void reset_impl();

private:
	byte*				m_buf;						/// buf
	byte*				m_header;					/// header pos
	byte*				m_tail;						/// tail pos
	int32				m_size;						/// buf size
	int32				m_used;						/// buf used
	uint64				m_llPos;					/// pos
	uint64				m_llTotal;					/// total write??
	bool				m_bEOF;						/// up stream is end of stream
	CBaseCritSec		m_cs;
};
